package com.mall.manager.authority.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.manager.authority.service.PartPowerService;
import com.mall.manager.authority.service.PowerService;
import com.mall.untils.Page;

/**
 * 角色权限表
 * @author liugb
 * @datetime 2016 7 7
 */
@Controller
@RequestMapping("/manager")
public class PartPowerController {
	Logger logger = LoggerFactory.getLogger(PartPowerController.class);
	@Resource PartPowerService partPowerService;
	@Resource PowerService powerService;
	
	   /**
	    * 角色权限信息列表
	    * @param request
	    * @param response
	    * @param modelMap
	    * @return
	    */
	  @SuppressWarnings("unchecked")
	@RequestMapping(value = "/manager_tpartpower_list")
	    public String manager_tpartpower_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Page page=partPowerService.manager_tpartpower_list(request);
	     	if(null!=page){
		     	modelMap.addAttribute("manager_tpartpower_list", (List<Map<String,Object>>)page.getResult());
		     	int totalpagecount = page.getTotalPageCount();
		     	modelMap.addAttribute("totalpagecount", totalpagecount);
		     	int currentpageno = page.getCurrentPageNo();
		     	modelMap.addAttribute("currentpageno", currentpageno);
		     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
						.getCurrentPageNo() - 1;
		     	modelMap.addAttribute("previouspage", previouspage);
		     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
						.getTotalPageCount() : page.getCurrentPageNo() + 1;
				modelMap.addAttribute("nextpage", nextpage);
				List<Integer> showNumList = page.getShowNumList();
				modelMap.addAttribute("pagenumlist", showNumList);
	     	}
	    	return "manager_tpartpower_list";
	    }
	  /**
	     * 删除角色权限表
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_del_tpartpower")    
	    @ResponseBody
	    public String manager_del_tpartpower(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	int update=partPowerService.manager_del_tpartpower(paraMap);
	    	return String.valueOf(update);
	    }
	    
	    /**
	     * 更改用户表
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_update_tpartpower")    
	    @ResponseBody
	    public String manager_update_tpartpower(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	int update=partPowerService.manager_update_tpartpower(paraMap);
	    	return String.valueOf(update);
	    }	  	
	    /**
	     * 跳转到角色权限页面
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_limit_tpart",method=RequestMethod.GET)   
	    public String manager_limit_Tpart(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	int roleid = MapUtils.getIntValue(paraMap, "id");
	    	List<Map<String, Object>> rst = partPowerService.findByRoleId(roleid); //获取本角色所有的权限
	    	if(!rst.isEmpty()){
	    		modelMap.addAttribute("rst", rst);
	    	}
	    	List<Map<String, Object>> rs1 = powerService.searchlistpower(1);
	    	List<Map<String, Object>> rs2 = powerService.searchlistpower(2);
	    	List<Map<String, Object>> rs3 = powerService.searchlistpower(3);
	    	modelMap.addAttribute("id", roleid);
	    	modelMap.addAttribute("rs1", rs1);
	    	modelMap.addAttribute("rs3", rs3);
		    modelMap.addAttribute("rs2", rs2);
	    	return "roles/role_limits";
	    }
	    /**
	     * 保存角色权限信息
	     * @param request
	     * @param id
	     * @param s1
	     * @param s2
	     * @param s3
	     * @return
	     */
	    @RequestMapping(value = "/insert_tpartpower",method=RequestMethod.POST)    
	    @ResponseBody
	    public String insert_tpartpower(HttpServletRequest request,int id,String[]s1,String[]s2,String[]s3) {
	    	List<Map<String,Object>> rs = partPowerService.findByRoleId(id);
	    	if(rs.isEmpty()){
	    		int update = 0;
	    		for (int i = 0; i < s1.length; i++) {
	    			if(!s1[i].equals("")){
	    				update = partPowerService.manager_insert_tpartpower(id,Integer.parseInt(s1[i]));    			
	    			}
	    		}
	    		for (int i = 0; i < s2.length; i++) {
	    			if(!s2[i].equals("")){
	    				update = partPowerService.manager_insert_tpartpower(id, Integer.parseInt(s2[i]));	    			
	    			}
	    		}
	    		for (int i = 0; i < s3.length; i++) {
	    			if(!s3[i].equals("")){
	    				update = partPowerService.manager_insert_tpartpower(id, Integer.parseInt(s3[i]));	    			
	    			}
	    		}
	    		return String.valueOf(update);	    		
	    	}else{
	    		Map<String, Object> map = new HashMap<String, Object>();
	    		map.put("roleId", id);
	    		int flag = partPowerService.manager_del_tpartpowerByRoleId(map);
	    		if(flag!=0){
	    			int update = 0;
		    		for (int i = 0; i < s1.length; i++) {
		    			if(!s1[i].equals("")){
		    				update = partPowerService.manager_insert_tpartpower(id,Integer.parseInt(s1[i]));    			
		    			}
		    		}
		    		for (int i = 0; i < s2.length; i++) {
		    			if(!s2[i].equals("")){
		    				update = partPowerService.manager_insert_tpartpower(id, Integer.parseInt(s2[i]));	    			
		    			}
		    		}
		    		for (int i = 0; i < s3.length; i++) {
		    			if(!s3[i].equals("")){
		    				update = partPowerService.manager_insert_tpartpower(id, Integer.parseInt(s3[i]));	    			
		    			}
		    		}
		    		return String.valueOf(update);	  
	    		}
	    		return null;
	    	}
	    }
	  
}
